package com.ease.framework.gef4p.model {
   import com.ease.util.collections.HashMap;
   import com.ease.util.collections.Map;
   import com.ease.common.models.Model;
   
   import mx.collections.ArrayCollection;

   [Bindable]
   public class Pool extends Model implements IArtifactsContainer {
      private var _id:String = "";
      public var name:String = "";
      public var desc:String = "";

      private var _nodes:ArrayCollection = new ArrayCollection();
      private var edges:ArrayCollection = new ArrayCollection();
      private var artifacts:ArrayCollection = new ArrayCollection();

      private var lanes:ArrayCollection = new ArrayCollection();

      public var hasLane:Boolean = false;
      public var laneType:String = null;
      public var parentNode:Node;

      private var mapNodes:Map = new HashMap();
      private var mapEdges:Map = new HashMap();
      private var mapArtifacts:Map = new HashMap();

      public function Pool() {
      }

      public function get nodes():ArrayCollection {
         return _nodes;
      }

      public function addNode(node:Node):void {
         nodes.addItem(node);
         mapNodes.put(node.id, node);
         node.pool = this;
      }

      public function removeNode(node:Node):void {
         if (nodes.contains(node)) {
            nodes.removeItemAt(nodes.getItemIndex(node));
            mapNodes.remove(node.id);
            node.pool = null;
         }
      }

      public function removeNodes():void {
         nodes.removeAll();
         mapNodes.clear();
      }

      public function getNode(id:String):Node {
         return mapNodes.getObject(id) as Node;
      }

      public function getEdges():ArrayCollection {
         return edges;
      }

      public function addEdge(edge:Edge):void {
         edges.addItem(edge);
         mapEdges.put(edge.id, edge);
         edge.pool = this;
      }

      public function removeEdge(edge:Edge):void {
         if (edges.contains(edge)) {
            edges.removeItemAt(edges.getItemIndex(edge));
            mapEdges.remove(edge.id);
            edge.pool = null;
         }
      }

      public function removeEdges():void {
         edges.removeAll();
         mapEdges.clear();
      }

      public function getEdge(id:String):Edge {
         return mapEdges.getObject(id) as Edge;
      }

      public function getArtifacts():ArrayCollection {
         return artifacts;
      }

      public function addArtifact(annotation:Artifact):void {
         artifacts.addItem(annotation);
         mapArtifacts.put(annotation.id, annotation);
         annotation.container = this;
      }

      public function removeArtifact(annotation:Artifact):void {
         if (artifacts.contains(annotation)) {
            artifacts.removeItemAt(artifacts.getItemIndex(annotation));
            mapArtifacts.remove(annotation.id);
            annotation.container = null;
         }
      }

      public function removeArtifacts():void {
         artifacts.removeAll();
         mapArtifacts.clear();
      }

      public function getArtifact(id:String):Artifact {
         return mapArtifacts.getObject(id) as Artifact;
      }

      public function get id():String {
         return _id;
      }

      public function set id(value:String):void {
         _id = value;
      }

   /*		public function getLanes():ArrayCollection{
      return lanes;
      }
      public function addLane(lane:Lane):void{

      //此处应该计算一下lane的位置，以及对其他Lane的影响.


      lanes.addItem( lane );
      lane.pool = this;
      }
      public function removeLane(lane:Lane):void{
      if( lanes.contains(lane) ){
      lanes.removeItemAt( lanes.getItemIndex( lane ) );
      lane.pool = null;
      }
      }
      public function removeLanes():void{
      lanes.removeAll();
      }

      /**
    * 按照当前Lane位置由上到下来排列.
    * @return
    */ /*		public function getSortedLanes():ArrayCollection{

      var dataSortField_y:SortField = new SortField();

      //需要根据当前Lane type，来判断排序字段.
      if(laneType==DiagramConstant.LANE_TYPE_VERTICAL){
      dataSortField_y.name = "y";
      }else{
      dataSortField_y.name = "x";
      }
      dataSortField_y.numeric = true;

      var numericDataSort:Sort = new Sort();

      numericDataSort.fields=[dataSortField_y];

      lanes.sort = numericDataSort;
      lanes.refresh();
      return lanes;
      }
    */
   }
}